package org.mspring.mlog.service;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.mspring.mlog.dao.PostDao;
import org.mspring.mlog.entity.Post;
import org.mspring.mlog.entity.constants.CacheNames;
import org.mspring.platform.cache.Cache;
import org.mspring.platform.cache.CacheManager;
import org.mspring.platform.dao.Order;
import org.mspring.platform.dao.Page;
import org.mspring.platform.dao.hibernate.common.CriteriaQuery;
import org.mspring.platform.dao.query.QueryBuilder;
import org.mspring.platform.security.SecurityUtils;
import org.mspring.platform.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Gao Youbo
 * @since 2014-01-11
 */
@Service
@Transactional
public class PostService {

    private PostDao postDao;
    private CacheManager cacheManager;
    private Cache indexPostListCache;

    /**
     * 最新文章
     *
     * @param nums
     * @return
     */
    public List<Post> recent(int nums) {
        CriteriaQuery criteriaQuery = new CriteriaQuery(Post.class);
        criteriaQuery.add(Restrictions.eq("status", Post.Status.PUBLISH));
        criteriaQuery.addOrder(new Order("createTime", Order.OrderType.DESC));
        criteriaQuery.setMaxResults(nums);
        return postDao.list(criteriaQuery);
    }

    /**
     * 热门文章
     *
     * @param nums
     * @param start
     * @param end
     * @return
     */
    public List<Post> hot(int nums, Date start, Date end) {
        CriteriaQuery criteriaQuery = new CriteriaQuery(Post.class);
        criteriaQuery.add(Restrictions.eq("status", Post.Status.PUBLISH));
        criteriaQuery.addOrder(new Order("views", Order.OrderType.DESC));
        criteriaQuery.setMaxResults(nums);
        if (start != null) {
            criteriaQuery.add(Restrictions.ge("createTime", start));
        }
        if (end != null) {
            criteriaQuery.add(Restrictions.le("createTime", end));
        }
        return postDao.list(criteriaQuery);
    }

    /**
     * 根据分类分页查询
     *
     * @param page
     * @param catalog
     * @return
     */
    public Page<Post> listByCatalog(Page<Post> page, long catalog) {
        return postDao.listByCatalog(page, catalog);
    }

    /**
     * 根据tag分页查询
     *
     * @param page
     * @param tag
     * @return
     */
    public Page<Post> listByTag(Page<Post> page, long tag) {
        return postDao.listByTag(page, tag);
    }

    /**
     * 统计总共有多少文章，只统计状态为已发布的
     *
     * @return
     */
    public long countPost() {
        return postDao.countPost();
    }

    public Post create(Post post) {
        if (post.getCreateTime() == null) {
            post.setCreateTime(new Date());
        }
        if (StringUtils.isBlank(post.getCommentStatus())) {
            post.setCommentStatus(Post.CommentStatus.OPEN);
        }
        if (StringUtils.isBlank(post.getStatus())) {
            post.setStatus(Post.Status.PUBLISH);
        }
        if (post.getReplys() == null) {
            post.setReplys(new Long(0));
        }
        if (post.getIsTop() == null) {
            post.setIsTop(false);
        }
        if (post.getAuthor() == null) {
            post.setAuthor(SecurityUtils.getCurrentUser());
        }
        post.setReplys(new Long(0));
        post.setViews(new Long(0));
        post = postDao.merge(post);
        if (post != null) {
            indexPostListCache.clear();
        }
        return post;
    }

    public void update(Post post) {
        postDao.update(post);
        indexPostListCache.clear();
    }

    public void delete(Long... id) {
        postDao.delete(id);
        indexPostListCache.clear();
    }

    public void delete(Post post) {
        postDao.delete(post);
        indexPostListCache.clear();
    }

    public void updateStatus(long id, String status) {
        int flag = postDao.updateStatus(id, status);
        if (flag > 0) {
            indexPostListCache.clear();
        }
    }

    public int updateViews() {
        return postDao.updateViews();
    }

    public int updateReplys() {
        return postDao.updateReplys();
    }

    public void clearTrash() {
        postDao.deleteByStatus(Post.Status.TRASH);
    }

    public Post get(Long id) {
        return postDao.get(id);
    }

    public Page<Post> listPage(QueryBuilder queryBuilder, Page<Post> postPage) {
        return postDao.listPage(queryBuilder, postPage);
    }

    public Page<Post> listPage(CriteriaQuery criteriaQuery, Page<Post> page) {
        return postDao.listPage(criteriaQuery, page);
    }

    public List<Post> list(DetachedCriteria detachedCriteria, int firstResult, int maxResults) {
        return postDao.list(detachedCriteria, firstResult, maxResults);
    }

    @Autowired
    public void setPostDao(PostDao postDao) {
        this.postDao = postDao;
    }

    @Autowired
    public void setCacheManager(CacheManager cacheManager) {
        this.cacheManager = cacheManager;
        this.indexPostListCache = this.cacheManager.getCache(CacheNames.INDEX_POST_LIST_CACHE);
    }

}
